<template>
  <div>
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>排序方式</p>
          <el-select v-model="selectedMsg.DistrictType" placeholder="请选择" size="mini" filterable @change="getPageList">
            <el-option v-for="item in dict.districtType" :key="item.code" :label="item.value" :value="item.code" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <p>名称</p>
          <el-input v-model="selectedMsg.Keyword" placeholder="请输入名称" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="16" style="text-align:right;">
          <el-button style="margin-top:27px" type="primary" size="mini" @click="getPageList()">{{ $t('button.query') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <el-row>
      <el-button style="margin-top:10px" type="primary" :loading="buLoading" size="mini" @click="reportData">导出积分表</el-button>
    </el-row>
    <el-table v-loading="loading" size="mini" :data="tableData" class="gl_table_noBorder gl_mt_10 gl_mb_15" style="width: 100%">
      <el-table-column align="center" :width="40">
        <template slot-scope="{ row }">
          <div v-if="row.finalRank === 1" class="allNumber numberOne" />
          <div v-else-if="row.finalRank === 2" class="allNumber numberTwo" />
          <div v-else-if="row.finalRank === 3" class="allNumber numberThree" />
          <div v-else class="allNumber" />
        </template>
      </el-table-column>
      <el-table-column label="排名" prop="finalRank" :width="80" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="goldCount" label="金牌" />
      <el-table-column prop="silverCount" label="银牌" />
      <el-table-column prop="bronzeCount" label="铜牌" />
      <el-table-column prop="realPoints" label="区域总分" />
      <el-table-column prop="finalPoints" label="实际展示总分" />
      <el-table-column width="100" label="操作">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="clickEidt(row)">积分修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit-integral ref="editIntegral" />
    <!-- 分页 -->
    <el-row style="text-align: right;">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="selectedMsg.MaxResultCount"
        :current-page="selectedMsg.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
  </div>
</template>

<script>
import { api_GET_DownLoadFileAddHeader } from '@/extend/api/file';
import { api_GET_EventsManagementEventResultPlatformPointRankList } from '@/extend/api/gameswaggerApi/app';
import { Api_GET_EventsManagementEventResultPlatformPointRankListQO } from '@/lib/form/game/swaggerForm';
import { mapState, mapActions } from 'vuex';
import editIntegral from '../editIntegral.vue';
import { errorText } from '@/lib';
export default {
  components: { editIntegral },
  data () {
    return {
      selectedMsg: new Api_GET_EventsManagementEventResultPlatformPointRankListQO(),
      tableData: [],
      loading: false,
      total: 0,
      buLoading: false
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      cache: state => state.dict.cache
    })
  },
  created() {
    this.getDict();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict', getDictByType: 'dict/getDictByType' }),
    getPageList(page) {
      this.selectedMsg.Type = 'District';
      this.loading = true;
      this.selectedMsg.Page = page || 1;
      this.selectedMsg.SkipCount = (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount;
      this.selectedMsg.DistrictType = this.selectedMsg.DistrictType || this.dict.districtType[0].code;
      api_GET_EventsManagementEventResultPlatformPointRankList(this.selectedMsg).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    resetData() {
      this.selectedMsg.Keyword = null;
      this.selectedMsg.DistrictType = this.dict.districtType[0].code;
      this.getPageList();
    },
    clickEidt(row) {
      this.$refs.editIntegral.dialogVisible = true;
      this.$refs.editIntegral.selectedMsg.id = row.id;
      this.$set(this.$refs.editIntegral.selectedMsg, 'realPoints', row.realPoints);
    },
    // 数据导出
    reportData() {
      var that = this;
      that.buLoading = true;
      const tempUrl = `/api/excel`;
      const fileName = '区域积分排名' + '.xlsx';
      const params = {};
      params.ReportName = 'PlatformRank';
      params.Type = 'District';
      params.DistrictType = this.selectedMsg.DistrictType || this.dict.districtType[0].code;
      params.Keyword = that.selectedMsg.Keyword || '';
      api_GET_DownLoadFileAddHeader(tempUrl, fileName, params).then(res => {
        that.buLoading = false;
      }).catch(error => {
        that.buLoading = false;
        errorText(error).then(function(text) {
          that.$message({ type: 'error', message: text });
        });
      });
    },
    // 分页
    changePage(page) {
      this.selectedMsg.Page = page;
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.selectedMsg.MaxResultCount = pagesize;
      this.getPageList();
    },
    // 获取静态图片
    getStaticFileUrl(relativeUrl) {
      if (relativeUrl) {
        return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.allNumber {
  font-size: 24px;
  font-weight: 700;
  text-align: center;
  color: #353434;
  width: 20px;
  height: 20px;
  line-height: 20px;
}
.numberOne {
  color: white;
  background: url('../../../../assets/images/integral_gold.png') no-repeat;
  background-size: 20px 20px;
}
.numberTwo {
  color: white;
  background: url('../../../../assets/images/integral_silver.png') no-repeat;
  background-size: 20px 20px;
}
.numberThree {
  color: white;
  background: url('../../../../assets/images/integral_bronze.png') no-repeat;
  background-size: 20px 20px;
}
.table_photo {
  width: 60px;
  height: 60px;
  border-radius: 6px;
}
.table_name {
  line-height: 60px;
}
</style>
